package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.icss.beans.CoreCourseVO;
import com.icss.daos.CoreCourseDao;


/**
 * @author 韦锦良
 *　类的作用：核心日程逻辑处理类
 * 最后修改时间：2010-3-13　下午03:56:35
 * 最后的改动内容：初始版本
 */
@SuppressWarnings("serial")
public class CoreCourseServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");

		CoreCourseDao dao = new CoreCourseDao();

		//添加核心课程
		if ("add".equals(action)) {

			String courseName = request.getParameter("courseName").trim();
			String courseRemark = request.getParameter("courseRemark").trim();
			String courseIntro = request.getParameter("courseIntro");

			String sql = "insert into coreCourse(courseName, courseRemark,courseIntro) values('" + courseName + "','"
					+ courseRemark + "','" + courseIntro + "')";

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();
			response.setContentType("text/html");

			if (result) {
				out.print("<script>alert('添加成功');window.location.href='coreCourse?action=admin_list';</script>");
			} else {
				out.print("<script>alert('添加失败');window.location.href='coreCourse?action=admin_list';</script>");
			}
			out.close();

		}

		//删除核心课程
		if ("delete".equals(action)) {

			String courseID = request.getParameter("courseID");

			String sql = "delete from CoreCourse where  courseID=" + courseID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			if (result) {
				out.print("<script>alert('删除成功');window.location.href='coreCourse?action=admin_list';</script>");
			} else {
				out.print("<script>alert('删除失败');window.location.href='coreCourse?action=admin_list';</script>");
			}
			out.close();

		}

		//修改核心课程
		if ("update".equals(action)) {

			String courseID = request.getParameter("courseID");
			String courseName = request.getParameter("courseName").trim();
			String courseRemark = request.getParameter("courseRemark").trim();
			String courseIntro = request.getParameter("courseIntro");

			String sql = "update CoreCourse set courseName='" + courseName + "',courseRemark='" + courseRemark
					+ "',courseIntro='" + courseIntro + "' where courseID=" + courseID;

			boolean result = dao.execute(sql);

			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			if (result) {
				out.print("<script>alert('修改成功');window.location.href='coreCourse?action=admin_list';</script>");
			} else {
				out.print("<script>alert('修改失败');window.location.href='coreCourse?action=admin_list';</script>");
			}
			out.close();
		}

		//管理员请求核心课程列表
		if ("admin_list".equals(action)) {

			ArrayList<CoreCourseVO> vos = dao.getAllTitle();
			request.setAttribute("admin_list", vos);
			request.getRequestDispatcher("/page/kecheng/admin_list.jsp").forward(request, response);
		}

		//显示核心课程
		if ("showKecheng".equals(action)) {

			String courseID = request.getParameter("courseID");
			CoreCourseVO vo = dao.getCoreCourseVOById(courseID);
			JSONObject coreCourse = JSONObject.fromObject(vo);
			PrintWriter out = response.getWriter();
			out.print(coreCourse);
			out.close();

		}
		
		//请求修改核心课程
		if ("updateRequest".equals(action)) {

			String courseID = request.getParameter("courseID");

			CoreCourseVO vo = dao.getCoreCourseVOById(courseID);

			JSONObject coreCourseVO = JSONObject.fromObject(vo);

			PrintWriter out = response.getWriter();

			out.print(coreCourseVO);

			out.close();

		}

		//前台请求核心课程
		if ("forward_list".equals(action)) {

			ArrayList<CoreCourseVO> vos = dao.getAllCoreCourseVO();

			request.setAttribute("forward_list", vos);

			request.getRequestDispatcher("/page/kecheng/forward_kecheng.jsp").forward(request, response);
		}

		//显示课程详细信息
		if ("showDetail".equals(action)) {

			String courseID = request.getParameter("courseID");

			CoreCourseVO vo = dao.getCoreCourseVOById(courseID);

			request.setAttribute("detail", vo);

			request.getRequestDispatcher("/page/kecheng/forward_detail.jsp").forward(request, response);

		}

		dao.close();
	}

}
